package com.snail.snailvr.views.fragments;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.snail.snailvr.VRApp;
import com.snail.snailvr.model.DownloadInfoChangeEvent;
import com.snail.snailvr.model.TaskInfo;
import com.snail.snailvr.model.VideoInfo;
import com.snail.snailvr.network.providers.downloads.DownloadHelper;
import com.snail.snailvr.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装下载进度条
 * Created by snail on 16/6/22.
 */

public abstract class BaseDownloadFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private List<VideoInfo> mVideoInfoList;

    protected abstract RecyclerView getRecyclerView();

    protected abstract RecyclerView.Adapter getAdapter();

    protected abstract List<VideoInfo> getVideoInfoList();

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = getRecyclerView();
        mAdapter = getAdapter();
        mVideoInfoList = getVideoInfoList();
    }

    /**
     * 更新列表数据下载状态
     */
    protected void updateDownloadState() {
        DownloadHelper.updateDownloadState(mVideoInfoList, mAdapter);
    }


    @Override
    public void onResume() {
        super.onResume();
        updateDownloadState();
    }


    protected void downloadInfoChange(DownloadInfoChangeEvent event) {
        //TODO 区分首页和发现页
        LogUtils.d("downloadInfoChange");
        ArrayList<TaskInfo> taskInfos = event.getTaskInfos();
        if (mRecyclerView != null && taskInfos != null) {
            synchronized (mVideoInfoList) {
                for (TaskInfo taskInfo : taskInfos) {
                    updateProgress(taskInfo, mVideoInfoList);
                }
            }

            if (mAdapter != null)
                mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 更新下载列表进度
     *
     * @param taskInfo
     * @param videoInfoList
     */
    private void updateProgress(TaskInfo taskInfo, List<VideoInfo> videoInfoList) {
        for (VideoInfo videoInfo : videoInfoList) {
            if (taskInfo.getTaskId() == videoInfo.getVideoId()||(videoInfo.getIBannerId()!=0&&taskInfo.getAppId()==videoInfo.getIBannerId())) {
                DownloadHelper.calcDownloadSpeed(VRApp.getInstance(), videoInfo, taskInfo);
                videoInfo.setVideoId(taskInfo.getTaskId());
                videoInfo.setVideoName(taskInfo.getTaskName());
                videoInfo.setVideoSize(taskInfo.getVideoTotalSize());
                videoInfo.setDownloadedSize(taskInfo.getDownloadedSize());
                videoInfo.setDownloadedPercent(taskInfo.getTaskPercent());
                videoInfo.setDownloadState(taskInfo.getDownloadState());
                LogUtils.d("local uri="+taskInfo.getVideoLocalUri());
                LogUtils.d("local uri="+videoInfo.getVideoPath());
//                videoInfo.setVideoPath(taskInfo.getVideoLocalUri());


                int reason = taskInfo.getReason();
                videoInfo.setReason(reason);
                DownloadHelper.handleMsgForPauseOrError(VRApp.getInstance(), videoInfo.getVideoName(),
                        taskInfo.getDownloadState(), reason);
                break;
            }
        }
    }

}
